﻿
CREATE PROCEDURE [dbo].[proc_Employee_Search_UserName]
	(
		@UserName nvarchar(50),
		@SearchText nvarchar(50),
		@type nvarchar(50)
	)
AS
begin
	Declare @companyId int
	EXEC @companyId=proc_Company_GetID @UserName,@companyId
	if(@type='local')
		begin
		Select
		Id,
		UserName,
		EName,
		CompanyId,
		DepartmentId,
		Isnull((Select PostName From Post Where Id=Employee.PostId),'...') As PostName,
		(Select CompanyName From Company Where Id=Employee.CompanyId) As CompanyName,
		Isnull((Select DName From Department Where Id=Employee.DepartmentId),'...') As DepartmentName
		From Employee Where CompanyId=@companyId and DepartmentId<>0 and  (@SearchText is null Or EName like '%'+@SearchText+'%') --and State<>5
		end
	else
		begin
		Select
		Id,
		UserName,
		EName,
		CompanyId,
		DepartmentId,
		Isnull((Select PostName From Post Where Id=Employee.PostId),'...') As PostName,
		(Select CompanyName From Company Where Id=Employee.CompanyId) As CompanyName,
		Isnull((Select DName From Department Where Id=Employee.DepartmentId),'...') As DepartmentName
		From Employee Where DepartmentId<>0 and (@SearchText is null Or EName like '%'+@SearchText+'%') --and State<>5
		end
end
	RETURN

